/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelFallasEmpleado.java
 *
 * Created on 11/04/2011, 08:14:28 PM
 */

package frontera.interfaz.panel;

import control.SIRHApp;
import entidad.Empleado;
import entidad.Falla;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Sebastian
 */
public class PanelFallasEmpleado extends javax.swing.JPanel {

    /** Creates new form PanelFallasEmpleado */
    public PanelFallasEmpleado(Empleado empleado,List<Falla> fallasTemporales,JPanel panel) {
        initComponents();
        iniciarVariables(empleado,fallasTemporales);
        iniciarCalendario();
        this.panel=panel;

    }

    private void iniciarVariables(Empleado empleado,List<Falla> fallasTemporales){
        this.empleado=empleado;
        labelNombre.setText(empleado.getNombre());
        this.fallasTemporales = fallasTemporales;
        labelNFallas.setText(Integer.toString(fallasTemporales.size()));
    }

    private void iniciarCalendario(){

        ActionListener listener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                actualizarCalendario((JButton)e.getSource());
            }
        };

        PanelCalendario calendario = new PanelCalendario(listener);
        panelCalendario.add(calendario);
      
        for(Falla fa: fallasTemporales){
            int dia = fa.getFecha().get(Calendar.DATE);
            calendario.getBotones().get(dia-1).setBackground(Color.GREEN);
        }
    }

    private void actualizarCalendario(JButton boton){
        Calendar cal = new GregorianCalendar();

        String strAgregar =    "Desea agregar una falla a "+
                                    empleado.getNombre() +
                                    " el dia " +
                                    boton.getText() +
                                    "/"+
                                    Integer.toString(cal.get(Calendar.MONTH)+1)+
                                    "/"+
                                    Integer.toString(cal.get(Calendar.YEAR));

        String strEliminar =       "Desea eliminar una falla a "+
                                    empleado.getNombre() +
                                    " el dia " +
                                    boton.getText() +
                                    "/"+
                                    Integer.toString(cal.get(Calendar.MONTH)+1)+
                                    "/"+
                                    Integer.toString(cal.get(Calendar.YEAR));


        int diaFalla = Integer.parseInt(boton.getText());
        Falla falla = new Falla(diaFalla, empleado.getCedula());
        //falla.setEmpAsociado(SIRHApp.getInstance().getDAOEmpleado().getEmpleadoID(empleado.getID()));
        falla.setEmpAsociado(empleado);
        
        if(!existeFalla(falla)){

            if(JOptionPane.showConfirmDialog(SIRHApp.getInstance().getVentanaPrincipal(), strAgregar ,"Error",JOptionPane.WARNING_MESSAGE)==JOptionPane.OK_OPTION){

                boton.setBackground(Color.green);
                fallasTemporales.add(falla);

            }
            
        }
        else if(JOptionPane.showConfirmDialog(SIRHApp.getInstance().getVentanaPrincipal(), strEliminar ,"Error",JOptionPane.WARNING_MESSAGE)==JOptionPane.OK_OPTION){
            
            boton.setBackground(SIRHApp.getInstance().getVentanaPrincipal().getBackground());
            borrarFalla(boton.getText());
            
        }
        
        labelNFallas.setText(Integer.toString(fallasTemporales.size()));
        
    }

    private boolean existeFalla(Falla falla){
        for(Falla fa: fallasTemporales){
            if(fa.getFecha().get(Calendar.DATE) == falla.getFecha().get(Calendar.DATE)){
                return true;
            }
        }
        return false;
    }

    private void borrarFalla(String diaDelMes){
        int size = fallasTemporales.size();
        int indice=-1;
        for(int i=0;i<size;i++){
            if(fallasTemporales.get(i).getFecha().get(Calendar.DATE) == Integer.parseInt(diaDelMes)){
                indice=i;
            }
        }
        if(indice!=-1){
            fallasTemporales.remove(indice);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        botonAtras = new javax.swing.JButton();
        labelIndicacion = new javax.swing.JLabel();
        panelTitulo = new javax.swing.JPanel();
        iconoFallas = new javax.swing.JLabel();
        labelTituloNombre = new javax.swing.JLabel();
        titulo = new javax.swing.JLabel();
        labelNombre = new javax.swing.JLabel();
        panelCalendario = new javax.swing.JPanel();
        panelMostrarFallas = new javax.swing.JPanel();
        labelTituloNFallas = new javax.swing.JLabel();
        labelNFallas = new javax.swing.JLabel();

        botonAtras.setText("Atras");
        botonAtras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAtrasActionPerformed(evt);
            }
        });

        labelIndicacion.setFont(new java.awt.Font("Tahoma", 0, 14));
        labelIndicacion.setText("Edite las fallas haciendo clic en el dia que desea modificar:");

        iconoFallas.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/falla.png"))); // NOI18N
        iconoFallas.setText(" ");

        labelTituloNombre.setText("Nombre");

        titulo.setFont(new java.awt.Font("Tahoma", 0, 26));
        titulo.setText("Editando Fallas ");

        labelNombre.setFont(new java.awt.Font("Tahoma", 0, 24));
        labelNombre.setText("jLabel6");

        javax.swing.GroupLayout panelTituloLayout = new javax.swing.GroupLayout(panelTitulo);
        panelTitulo.setLayout(panelTituloLayout);
        panelTituloLayout.setHorizontalGroup(
            panelTituloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelTituloLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(iconoFallas, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelTituloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelTituloNombre)
                    .addComponent(titulo)
                    .addComponent(labelNombre))
                .addContainerGap())
        );
        panelTituloLayout.setVerticalGroup(
            panelTituloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelTituloLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelTituloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(panelTituloLayout.createSequentialGroup()
                        .addComponent(titulo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelTituloNombre)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelNombre))
                    .addComponent(iconoFallas))
                .addContainerGap())
        );

        panelCalendario.setLayout(new java.awt.GridLayout());

        labelTituloNFallas.setText("Numero de Fallas en el Mes:");

        labelNFallas.setText("Numero de Fallas");

        javax.swing.GroupLayout panelMostrarFallasLayout = new javax.swing.GroupLayout(panelMostrarFallas);
        panelMostrarFallas.setLayout(panelMostrarFallasLayout);
        panelMostrarFallasLayout.setHorizontalGroup(
            panelMostrarFallasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelMostrarFallasLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelTituloNFallas)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelNFallas)
                .addContainerGap(358, Short.MAX_VALUE))
        );
        panelMostrarFallasLayout.setVerticalGroup(
            panelMostrarFallasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelMostrarFallasLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(panelMostrarFallasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelTituloNFallas)
                    .addComponent(labelNFallas)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(botonAtras)
                    .addComponent(panelMostrarFallas, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelTitulo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelIndicacion, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelCalendario, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 590, Short.MAX_VALUE))
                .addGap(90, 90, 90))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelIndicacion)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelCalendario, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(panelMostrarFallas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(botonAtras)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void botonAtrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAtrasActionPerformed

        SIRHApp.getInstance().getVentanaPrincipal().setPanelPersonalEmpleado(panel);
        
    }//GEN-LAST:event_botonAtrasActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAtras;
    private javax.swing.JLabel iconoFallas;
    private javax.swing.JLabel labelIndicacion;
    private javax.swing.JLabel labelNFallas;
    private javax.swing.JLabel labelNombre;
    private javax.swing.JLabel labelTituloNFallas;
    private javax.swing.JLabel labelTituloNombre;
    private javax.swing.JPanel panelCalendario;
    private javax.swing.JPanel panelMostrarFallas;
    private javax.swing.JPanel panelTitulo;
    private javax.swing.JLabel titulo;
    // End of variables declaration//GEN-END:variables
    private Empleado empleado;  
    private List<Falla> fallasTemporales;
    private JPanel panel;
    

}
